package com.aw.baselib.util

/**
 * Created by hzw
 */
class AwFormatCheckUtils private constructor() {
    init {
        /* cannot be instantiated */
        throw UnsupportedOperationException("cannot be instantiated")
    }

    companion object {
        /**
         * 检测是否只有字母或者数字
         *
         * @param str
         * @return
         */
        fun isLetterDigit(str: String): Boolean {
            var isRight = false //定义一个boolean值，用来表示是否包含数字
            val regex = "^[a-zA-Z0-9]+$"
            isRight = str.matches(regex.toRegex())
            return isRight
        }

        /**
         * 必须包含字母和数字
         *
         * @param str
         * @return
         */
        fun isLetterDigitAll(str: String): Boolean {
            var isRight = false //定义一个boolean值，用来表示是否包含数字
            val regex = ".*[a-zA-Z].*[0-9]|.*[0-9].*[a-zA-Z]"
            isRight = str.matches(regex.toRegex())
            return isRight
        }

        /**
         * 判断是否符合邮箱格式
         */
        fun checkEmailValid(strEmail: String?): Boolean {
            return strEmail?.matches("[a-zA-Z0-9_]+@[a-z0-9]+(.[a-z]+){2}".toRegex()) ?: false
        }

        /**
         * 判断是否符合座机号格式
         *
         * @param phoneNumber
         * @return
         */
        fun checkPhoneNumberValid(phoneNumber: String?): Boolean {
            if (null == phoneNumber) {
                return false
            }
            /**
             * 匹配北京上海等3-8格式：(^0[1,2]{1}\\d{1}-?\\d{8}
             * 匹配其他省份等4-7/8格式:(^0[3-9]{1}\\d{2}-?\\d{7,8})
             * 匹配内部电话转接号：(-(\\d{1,4}))?$)
             */
            // 区号与座机号之间可不添加“-” 外部号码与内部号码之间必须添加“-”
            val check = "((^0[1,2]{1}\\d{1}-?\\d{8}|(^0[3-9]{1}\\d{2}-?\\d{7,8}))(-(\\d{1,4}))?$)"
            return phoneNumber.matches(check.toRegex())
        }

        /**
         * 验证手机号方法
         *
         * @param strPhoneNum
         * @return
         */
        fun checkMobileNumberValid(strPhoneNum: String?): Boolean {
            if (null == strPhoneNum) {
                return false
            }
            /**
             * 匹配13、15、18开头手机号 排除154 开头手机号
             * 匹配170、176、177、178开头手机号
             * 匹配规则参考当前（2015-04-29）百度百科“手机号”罗列号码
             */
            val checkphone = "^(((13|18)[0-9])|(15[^4,\\D])|170|176|177|178|199|198)\\d{8}$"
            return strPhoneNum.matches(checkphone.toRegex())
        }

        /**
         * 检测String是否全是中文
         *
         * @param name
         * @return
         */
        fun checkNameChese(name: String): Boolean {
            var res = true
            val cTemp = name.toCharArray()
            for (i in 0 until name.length) {
                if (!isChinese(cTemp[i])) {
                    res = false
                    break
                }
            }
            return res
        }

        /**
         * 判定输入汉字
         *
         * @param c
         * @return
         */
        fun isChinese(c: Char): Boolean {
            val ub = Character.UnicodeBlock.of(c)
            return if (ub === Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub === Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub === Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub === Character.UnicodeBlock.GENERAL_PUNCTUATION || ub === Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub === Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                true
            } else false
        }
    }
}